package net.chathu.airgw.packets.chat
{
	import flash.utils.Dictionary;
	import net.chathu.airgw.Config;
	import net.chathu.airgw.Debug;
	import net.chathu.airgw.model.JoinedRoomInfo;
	import net.chathu.airgw.model.Room;
	import net.chathu.airgw.model.User;
	import net.chathu.airgw.packets.gw.serverpackets.EchoPacket;
	import net.chathu.airgw.packets.gw.serverpackets.NickModePacket;
	import net.chathu.airgw.PrefixManager;
	import net.chathu.airgw.util.ByteArrayUtil;
	import net.chathu.airgw.util.StringUtil;
	
	/**
	 * ...
	 * @author ze
	 */
	public class MUserinfo extends ChatPacket
	{
		
		public function MUserinfo()
		{
		
		}
		
		override public function execute():void
		{
			var i:int;
			var regId:int;
			var userId:int;
			var userName:String;
			var user:User;
			if (data[3] == "status")
			{
				var options:String = String(data[4]).substr(1);
				sessionData.chatData.chatOptions.parseOptions(options);
				sessionData.chatData.chatOptions.acceptConfRoomInvite = parseInt(data[5]);
				sessionData.chatData.chatOptions.autoReplyText = data[7];
			}
			else if (data[3] == "contacts")
			{
				//baratok listaja erkezett
				i = 4;
				while (i + 1 < data.length)
				{
					
					userId = parseInt(data[i++]);
					userName = StringUtil.convertFromISO88591(data[i++]);
					//Debug.echo("Barát: id: " + userId + " név: " + userName);
					sessionData.chatData.addFriend(userId, userName);
				}
			}
			else if (data[3] == "ignores")
			{
				//tiltottak listaja erkezett
				i = 4;
				while (i + 1 < data.length)
				{
					
					userId = parseInt(data[i++]);
					userName = StringUtil.convertFromISO88591(data[i++]);
					sessionData.chatData.addIgnore(userId, userName);
				}
			}
			else if (data[3] == "ignoreadd")
			{
				regId = parseInt(data[4]);
				userName = StringUtil.convertFromISO88591(data[5]);
				handleListChangeRequest(data[3], regId, userName);
				sendToClient(new EchoPacket(Config.GW_ECHO_NICK, "Némítottak listája bővült: " + userName));
			}
			else if (data[3] == "ignoredel")
			{
				regId = parseInt(data[4]);
				user = sessionData.chatData.users.getUserByRegID(regId) as User;
				if (user)
				{
					userName = user.name;
				}
				else
				{
					userName = sessionData.chatData.ignoreList[regId];
				}
				
				handleListChangeRequest(data[3], regId, userName);
				if (userName) //ha nincs userName, akkor nem letezo felhasznalot probaltal torolni; ebben az esetben a handleList.. sem kellene amugy
				{
					
					sendToClient(new EchoPacket(Config.GW_ECHO_NICK, userName + " törölve a némítottak közül!"));
				}
			}
			else if (data[3] == "contadd")
			{
				regId = parseInt(data[4]);
				userName = StringUtil.convertFromISO88591(data[5]);
				handleListChangeRequest(data[3], regId, userName);
			}
			else if (data[3] == "contdel")
			{
				regId = parseInt(data[4]);
				userName = StringUtil.convertFromISO88591(data[5]);
				sendToClient(new EchoPacket(Config.GW_ECHO_NICK, userName + " törölve a barátok közül!"));
				handleListChangeRequest(data[3], regId, userName);
			}
			else if (data[3] == "userinfo")
			{
				//reszletes adatlap erkezett
				var text:String = data[data.length - 1] as String;
				if (text.indexOf("J") == 0) //Jelenleg-bol az elso betu
				{
					text = "j" + text.substring(1);
				}
				
				user = sessionData.chatData.users.getUserByRegID(parseInt(data[4])) as User;
				var nick:String = user.originalName;
				//nagy valoszinuseggel a data[5] encoding valahogy el van rontva
				//trukkok helyett inkabb az egyszerubb modszer:
				
				sendToClient(new EchoPacket(Config.GW_ECHO_NICK, "[ Userinfo: "+ Config.IRC_CHAR_COLOR + "4" + Config.IRC_CHAR_BOLD + Config.IRC_CHAR_BOLD + nick + Config.IRC_CHAR_COLOR + " ]"));
				sendToClient(new EchoPacket(Config.GW_ECHO_NICK, nick + StringUtil.convertFromISO88591(" " + text)));
				sendToClient(new EchoPacket(Config.GW_ECHO_NICK, "Születési idő: " + StringUtil.convertFromISO88591(data[7])));
				sendToClient(new EchoPacket(Config.GW_ECHO_NICK, "Ország: " + StringUtil.convertFromISO88591(data[10])));
				sendToClient(new EchoPacket(Config.GW_ECHO_NICK, "Weboldal: " + StringUtil.convertFromISO88591(data[data.length - 4])));
				sendToClient(new EchoPacket(Config.GW_ECHO_NICK, " "));
				sendToClient(new EchoPacket(Config.GW_ECHO_NICK, "Bemutatkozás: "));
				var about:String = StringUtil.convertFromISO88591(data[data.length - 3]);
				
				var lines:Array = about.split("\n");
				for (var b:int = 0; b < lines.length; b++)
				{
					sendToClient(new EchoPacket(Config.GW_ECHO_NICK, lines[b]));
				}
				sendToClient(new EchoPacket(Config.GW_ECHO_NICK, "[ End Of Userinfo ]"));
				
			}
			else
			{
				Debug.echo("Unhandled command: " + data.join("|"));
			}
		}
		
		private function handleListChangeRequest(command:String, regId:int, userName:String):void
		{
			var i:int;
			var user:User = sessionData.chatData.users.getUserByRegID(regId) as User;
			if (user)
			{
				
				var currMode:String;
				var room:JoinedRoomInfo;
				var rooms:Array = sessionData.chatData.getCommonJoinedRoomsWithUser(user.id);
				var modesBefore:Dictionary = new Dictionary();
				//taroljuk el valtas elott a modot, majd utana vessuk ossze az elozoekkel
				for (i = 0; i < rooms.length; i++)
				{
					room = rooms[i];
					currMode = PrefixManager.getUserModes(sessionData, user, room);
					modesBefore[room.roomId] = currMode;
				}
				if (sessionData.chatData.isInUsersRoom)
				{
					modesBefore[Config.USERSLIST_CHANNEL] = PrefixManager.getUserModes(sessionData, user, null);
				}
				
				if (command == "contadd")
				{
					sessionData.chatData.addFriend(regId, userName);
				}
				else if (command == "contdel")
				{
					sessionData.chatData.removeFriend(regId);
				}
				else if (command == "ignoreadd")
				{
					sessionData.chatData.addIgnore(regId, userName);
				}
				else if (command == "ignoredel")
				{
					sessionData.chatData.removeIgnore(regId);
				}
				
				for (i = 0; i < rooms.length; i++)
				{
					room = rooms[i];
					currMode = PrefixManager.getUserModes(sessionData, user, room);
					var r:Room = sessionData.chatData.rooms.getRoom(room.roomId);
					changeUserMode(r.name, user.name, modesBefore[room.roomId], currMode);
				}
				
				if (sessionData.chatData.isInUsersRoom)
				{
					//valtsunk modot a &users szobaban is!
					changeUserMode(Config.USERSLIST_CHANNEL, user.name, modesBefore[Config.USERSLIST_CHANNEL], PrefixManager.getUserModes(sessionData, user, null));
				}
			}
			else
			{
				if (command == "contdel")
				{
					sessionData.chatData.removeFriend(regId);
				}
				else if (command == "ignoredel")
				{
					sessionData.chatData.removeIgnore(regId);
				}
			}
		}
		
		private function changeUserMode(roomName:String, userName:String, modeBefore:String, modeAfter:String):void
		{
			if (modeAfter == modeBefore)
			{
				return;
			}
			
			if (modeBefore.length > 0)
			{
				sendToClient(new NickModePacket(roomName, userName, "-" + modeBefore)); //vegyuk el az elozo modot
			}
			if (modeAfter.length > 0)
			{
				sendToClient(new NickModePacket(roomName, userName, "+" + modeAfter)); //adjunk neki ujat
			}
		}
	
	}

}